{include file="header.tpl"}
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
rowNumber = 1;
$(document).ready(function()
{
  $(".delete").live('click', function(event) 
  {
    $(this).parent().remove();
    setFine(this);
    totFineFunc(this);
    setAmount(this);
    totAmount(this);
  });
  $(".grossWeight").live('change',function(){
    setWeight($(this));
  });
  $(".weightPerc").live('change',function(){
    setWeight($(this).prev());
  });
  getOldAmount();
});

function addNewRow()
{
  $('#bhatthi tr:last').after('<tr class="bhitthiRow"><td><input type="checkbox" name="rojmelEntry['+rowNumber+']" value = "1" /></td><td><input type="text" name="particulars[]" \/><\/td><td><input type="text" name="grossWeight[]" class="grossWeight"  size="13"/><input type="text" name="weightPerc[]" class="weightPerc" value="{$weightPerc}"  size="3"/><input type="text" name="weight[]" class="weight" onChange="setFine(this)"  size="13"\/><\/td><td><input type="text" name="touch[]" class="touch" onChange="setFine(this)"\/><\/td><td><input type="text" name="fine[]" class="fine"  onChange="totFine()"\/><\/td><td><input type="text" name="qty[]" class="qty" onChange="setAmount(this)"\/><\/td><td><input type="text" name="rate[]" class="rate" onChange="setAmount(this)"\/><\/td><td><input type="text" name="amount[]" class="amount"  onChange="totAmount()"\/><\/td><td><a href="javascript:addNewRow();">{$l_add}<\/a><\/td><td class="delete">{$l_remove}<\/td><\/tr>');
  rowNumber++;
}

function setFine(txtObj)
{
  var row = $(txtObj).parents('.bhitthiRow');
  var price = (parseFloat(row.find('.weight').val().replace("$","")) * parseFloat(row.find('.touch').val())/100);
  isNaN(price) ? row.find('.fine').val("") : row.find('.fine').val(price);
  totFine();
}
function totFine()
{
  var totalFine = 0;
  var prevFineWithoutThisBill = 0;
  $('.fine').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totalFine += Number(price);
  });
  $('#totalFine').val(totalFine);
  //prevFineWithoutThisBill = $("#prevFineWithoutThisBill").val();
  $('#partyFine').val(parseFloat(prevPartyFine) - parseFloat(totalFine));
}
// Add  Fine InTo Previous Fine :Start 
function totFineFunc()
{
  var totFine = 0;
  
  $('.fine').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totFine += Number(price);
  });
  
  $('#totFine').val(totFine);
  prevFineWithoutThisBill = $("#prevFineWithoutThisBill").val();
  $('#partyFineEdit').val(parseFloat(prevFineWithoutThisBill) - parseFloat(totalFine));
}
// Add  Fine InTo Previous Fine :End
function setWeight(grossWeight)
{
  valuesAvailable = 100;
  grossWeightVal = grossWeight.val();
  percentageVal  = grossWeight.next().val();
  grossWeightObj = grossWeight.next().next();
  grossWeightObj.val(((grossWeightVal * percentageVal) / valuesAvailable) + parseFloat(grossWeightVal));
}

// Add  Fine InTo Amount Fine :Start 

function setAmount(txtObj)
{
  var row = $(txtObj).parents('.bhitthiRow');
  var price = parseFloat(row.find('.qty').val().replace("$","")) * parseFloat(row.find('.rate').val());
  isNaN(price) ? row.find('.amount').val("") : row.find('.amount').val(price);
  totAmount();
  setTotAmount();
}
function setTotAmount()
{
  var totQty    = 0;
  var totLabour = 0;
  var totAmount = 0;
  
  $('.amount').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totAmount += Number(price);
  });
  $('#totAmount').val(totAmount);
  $('#partyAmount').val(parseFloat(prevPartyAmount) -  parseFloat(totAmount));
}

// Add  Fine InTo Amount Fine :End
function totAmount()
{
  var totAmount = 0;
  var totalQty  = 0;
  
  $('.amount').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totAmount += Number(price);
  });
  
  $('.qty').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totalQty += Number(price);
  });
  
  $('#totalQty').val(totalQty);
  $('#partyAmountEdit').val(totAmount);
}
function getOldAmount()
  {
	  if(document.form1.party.value > 0)
	  {
        $.ajax({
	      type:"POST",
	      url:'getOldAmountBhatthi.php',
	      data:
	      {
	        partyId : document.form1.party.value
	      },
	      success: function(response)
	      {
	      	var temp = response.split(",");
	      	prevPartyFine = temp[0];
	      	prevPartyAmount = temp[1];
	      	$('#partyFine').val(prevPartyFine);
	      	$('#partyAmount').val(prevPartyAmount);
	      }
      });
	  }
  }
</script>
<form name="form1" action="" method="post">
<input type="hidden" name="bhatthiId" value="{$bhatthiId}" />
<input type="hidden" name="prevFineWithoutThisBill" id="prevFineWithoutThisBill" value="{$prevFineWithoutThisBill}" />
<input type="hidden" name="prevAmountWithoutThisBill" id="prevAmountWithoutThisBill" value="{$prevAmountWithoutThisBill}" />
<center><h4>{$l_bhatthiCredit}</h4></center>
<div class="contentDiv">
  <table border="0" id="bhatthi" align="center" width="100%">
  <tr>
    <td colspan="4" class="table1">
      {$l_partySelect} :
      <select name="party"  onChange="getOldAmount();">
        {html_options values=$party.id output=$party.name selected="$bhatthiParty"}
      </select>
    </td>
    <td colspan="5" class="table1">
      {$l_date} : 
      {html_select_date prefix="curDate" start_year="-2" end_year="+1" field_order="DMY" day_value_format="%02d" time=$bhatthiDate}
    </td>
  </tr>
  <tr class="table1">
  	<th>&nbsp;</th>
    <th>{$l_particulars}</th>
    <th>{$l_grossWeight}&nbsp;&nbsp;&nbsp;&nbsp{$l_weightPerc};&nbsp;&nbsp;&nbsp;&nbsp;{$l_weight}</th>
    <th>{$l_touch}</th>
    <th>{$l_fine}</th>
    <th>{$l_qty}</th>
    <th>{$l_rate}</th>
    <th>{$l_amount}</th>
    
  </tr>
  {section name="sec" loop=$bhatthi|@count}
  <tr class="bhitthiRow">
  	{if $bhatthi[sec].rojmelId != 0}
    <td><input type="checkbox" name="rojmelEntry[{$smarty.section.sec.index}]" value= "1" checked /></td>
    {else}
    <td><input type="checkbox" name="rojmelEntry[{$smarty.section.sec.index}]" value= "1" /></td>
    {/if}
    <td><input type="text" name="particulars[]" value="{$bhatthi[sec].particulars}" /></td>
    <td><input type="text" name="grossWeight[]" class="grossWeight" value="{$bhatthi[sec].grossWeight}" size="13"/>
    	  <input type="text" name="weightPerc[]" class="weightPerc" value="{$weightPerc}" size="3" />
    	  <input type="text" name="weight[]" class="weight" onChange="setFine(this)" value="{$bhatthi[sec].weight}" size="13" /></td>
    <td><input type="text" name="touch[]" class="touch" onChange="setFine(this)" value="{$bhatthi[sec].touch}" /></td>
    <td><input type="text" name="fine[]" class="fine" onChange="totFineFunc()" value="{$bhatthi[sec].fine}" /></td>
    <td><input type="text" name="qty[]" class="qty" onChange="setAmount(this)" value="{$bhatthi[sec].qty}" /></td>
    <td><input type="text" name="rate[]" class="rate" onChange="setAmount(this)" value="{$bhatthi[sec].rate}" /></td>
    <td><input type="text" name="amount[]" class="amount" value="{$bhatthi[sec].amount}" /></td>
    <td><a href="javascript:addNewRow();">{$l_add}</a></td>
    <td class="delete">{$l_remove}</td>
    <td>&nbsp;</td>
  </tr>
  {/section}
  <tr class="bhitthiRow">
    <td><input type="checkbox" name="rojmelEntry[]" value= "1" /></td>
    <td><input type="text" name="particulars[]" /></td>
    <td><input type="text" name="grossWeight[]" class="grossWeight"  size="13"/><input type="text" name="weightPerc[]" class="weightPerc" value="{$weightPerc}"  size="3"/><input type="text" name="weight[]" class="weight" onChange="setFine(this)"  size="13"/></td>
    <td><input type="text" name="touch[]"  class="touch"  onChange="setFine(this)"/></td>
    <td><input type="text" name="fine[]"   class="fine"   onChange="totFine()" /></td>
    <td><input type="text" name="qty[]"    class="qty"    onChange="setAmount(this)"/></td>
    <td><input type="text" name="rate[]"   class="rate"   onChange="setAmount(this)"/></td>
    <td><input type="text" name="amount[]" class="amount" onChange="totAmount()"  onChange="setTotAmount()"/></td>
    <td><a href="javascript:addNewRow();">{$l_add}</a></td>
    <td>&nbsp;</td>
  </tr>
  </table>
  <table border="0" width="100%">
    <tr class="itemRowTot">
      <th width="150px;">{$l_total}</th>
      <th align="right" width="150px;">&nbsp;</th>
      <th align="right" width="150px;">&nbsp;</th>
      <th align="right"><input type="text" name="totalFine"   id="totalFine"   onChange="totFine()"/></th>
      <th align="right"><input type="text" name="totalQty"    id="totalQty"    onChange="totAmount()"/></th>
      <th align="right" width="150px;">&nbsp;</th>
      <th align="right"><input type="text" name="totalAmount" id="partyAmountEdit" onChange="totAmount()"/></th>
      <th colspan="2" width="100px;">&nbsp;</th>
    </tr>
  </table>
    {if $isEdit == 1}
    <div id="oldTotal" style="float:right;">
  	  <b>Prev. Fine</b> <input type="text" name="partyFine" id="partyFine" />
  	  <b>Prev. Amount</b> <input type='text' name='partyAmount' id='partyAmount' />
    </div>
  	{else}
    <div id="oldTotal" style="float:right;">
    	<b>Prev. Fine</b> <input type="text" name="partyFine" id="partyFine" />
  	  <b>Prev. Amount</b> <input type='text' name='partyAmount' id='partyAmount' />
    </div>
  	{/if}
  <input type="submit" name="bhatthiAddBtn" value="{if $bhatthi|@count > 0}{$l_save}{else}{$l_add}{/if}"/>
</div>
</form>
<script type="text/javascript">
{if $bhatthi|@count > 0}
  totAmount();
  totFine();
  totFineFunc();
  setTotAmount();
{/if}
</script>
{include file="footer.tpl"}